/**
 * 功能描述
 */

define(function (require, exports, module) {
	var HandlerBars = require("handlebars");
	var moment = require("moment");
	var calendarTd = require("../assets/tpl/calendar_td.tpl");
	var notset_calendar_td = require("../assets/tpl/notset_calendar_td.tpl");
	var roomTypeDataSvc = require("../store/roomTypeDataSvc");
	
    var instance = {
        initComponent: function (params, widget) {
        	this.renderCalendar(widget);
        	this.setCalendarData(widget);
        },
        //绘制日历框架
        renderCalendar:function(widget){
        	var selectDate = widget.get("monthRange").getValue();
        	var idate = moment(selectDate);
        	var startDateWeek = idate.day();
        	var monthDayNum = new Date(idate.year(),idate.month()+1,0).getDate();
        	var allTdNum = parseInt((monthDayNum+startDateWeek)/7+1)*7;
        	$(".J-calendar-table",widget.element).html("");
        	
        	//绘制日历
        	for(var i=0; i<allTdNum; i++){
        		var trIndex = parseInt(i/7);
        		if(i%7 == 0){
        			$(".J-calendar-table",widget.element).append("<tr class='tr_"+trIndex+"'></tr>");
        		}
        		if(i>startDateWeek-1 && i<=(monthDayNum+startDateWeek-1)){
        			$($(".J-calendar-table tr",widget.element)[trIndex]).append("<td class='td_"+(i-startDateWeek+1)+"'></td>");
        		}else{
        			$($(".J-calendar-table tr",widget.element)[trIndex]).append("<td></td>");
        		}
        	}
        },
        //设置日历数据
        setCalendarData:function(widget){
        	var selectDate = widget.get("monthRange").getValue();
        	
        	if(!selectDate){
        		return;
        	}
        	
        	var subnav = widget.get("subnav");
        	var pkServicePoint = subnav.getValue("servicepoint");
        	var pkOrgBuilding = subnav.getValue("buiding");
        	var pkRoomType = subnav.getValue("roomtypes");
        	var idate = moment(selectDate);
        	var monthDayNum = new Date(idate.year(),idate.month()+1,0).getDate();
        	
        	roomTypeDataSvc.getRoomTypesData(pkServicePoint,pkOrgBuilding,pkRoomType,selectDate,function(data){
        		$(".J-calendar-table td",widget.element).html("");
        		$(".J-calendar-table td",widget.element).removeClass("no-data-day");
        		for(var i=0;i<data.length;i++){
        			var idata = data[i];
        			idata.vipPrice = idata.vipPrice.toFixed(2);
        			idata.standardPrice = idata.standardPrice.toFixed(2);
        			idata.protocolPrice = idata.protocolPrice.toFixed(2);
        			idata.saleDate = moment(idata.saleDate).format("D");
        			var html = HandlerBars.compile(calendarTd)(idata);
        			$(".td_"+idata.saleDate,widget.element).html(html);
        		}
        		
        		for(var j=1;j<=monthDayNum;j++){
        			if(!$(".td_"+j,widget.element).html()){
        				var html = HandlerBars.compile(notset_calendar_td)({
            				saleDate:j
            			});
        				$(".td_"+j,widget.element).html(html);
        				$(".td_"+j,widget.element).addClass("no-data-day");
        			}
        		}
        		
        	});
        },
        //清除日历数据
        clearCalendar:function(widget){
        	var selectDate = widget.get("monthRange").getValue();
        	var idate = moment(selectDate);
        	var monthDayNum = new Date(idate.year(),idate.month(),0).getDate();
        	
        	for(var j=0;j<monthDayNum;j++){
    			$(".td_"+j,widget.element).html("<li>"+j+"</li>");
    		}
        	
        }
    };
    module.exports = instance;
});
